<?php

class Silk_Mapi_HomeController extends Silk_Mapi_Controller_Action {
	public function bannersAction()
	{
		$key = "home_banners_".Mage::app()->getStore()->getId();
		$data = apc_fetch($key);
		if($data)
		{
			$this->render($data);
			return;
		}

		$banners = Mage::getModel('bannerslider/banner')->getCollection()
			->addFieldToFilter('bannerslider_id', array('eq'=>13))
			->addFieldToFilter('status', array('eq'=>0))
			->setOrder('order_banner','asc');

		$data = array();

		foreach($banners as $banner)
		{
			$data[] = array(
					'image'=>Mage::helper('bannerslider')->getBannerImage($banner->getImage()),
					'name'=>$banner->getName(),
					'url'=>$banner->getClickUrl()
					
				);
		}

		apc_add($key, $data, 300);
		$this->render($data);
	}

	public function hotAction()
	{
		$catId = 10;
		$_imageHelper = Mage::helper('catalog/image');
		$collection = Mage::getModel('catalog/product')->getCollection()
            ->addAttributeToSelect('name')
            ->addAttributeToSelect('tag')
            ->addAttributeToSelect('price')
            ->addAttributeToSelect('special_price')
            ->addStoreFilter(Mage::app()->getStore()->getId())
            ->joinField('position',
                'catalog/category_product',
                'position',
                'product_id=entity_id',
                'category_id='.$catId,
                'left')
            ->setOrder('position','desc')
            ->setPageSize(10)
            ->setCurPage(1);
	    Mage::getSingleton('catalog/product_status')->addSaleableFilterToCollection($collection);
		Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($collection);
        Mage::getSingleton('cataloginventory/stock')->addInStockFilterToCollection($collection);

        $products = array();
        foreach($collection as $item)
        {
        	$products[] = array(
        		'id'=>$item->getId(),
        		'name'=>$item->getName(),
        		'tag'=>$item->getAttributeText('tag'),
        		'price'=>$item->getPrice()+0,
        		'special_price'=>$item->getFinalPrice(),
        		'image'=>$_imageHelper->init($item, 'small_image')->resize(250)."",
        		'position'=>$item->getPosition(),
        	);
        }
	    $this->render($products);
	}

	public function topAction()
	{
		$key = "home_top_".Mage::app()->getStore()->getId();
		$data = apc_fetch($key);
		if($data)
		{
			$this->render($data);
			return;
		}


		$_imageHelper = Mage::helper('catalog/image');
		$data = array();

		$catIds = array(14,5,6,8,9,10,11,13);

		foreach($catIds as $catId)
		{
			$category = Mage::getModel('catalog/category')->load($catId);
			if ($catId == 14) {
				$store = Mage::app()->getStore();
				$collection = Mage::getModel('catalog/product')->getCollection()->setStoreId($store->getId())
					->addWebsiteFilter($store->getWebsiteId())
					->addAttributeToSelect('*')
					->addAttributeToFilter('tag', Silk_Qslb_Helper_Tag::EXPLOSION)
					->setOrder('tag_position', 'desc')
					->setPageSize(6)
					->setCurPage(1);
			} else {
				$collection = Mage::getModel('catalog/product')->getCollection()
					->addAttributeToSelect('name')
					->addAttributeToSelect('tag')
					->addAttributeToSelect('price')
					->addAttributeToSelect('special_price')
					->addAttributeToSelect('special_price_from')
					->addAttributeToSelect('special_price_end')
					->addAttributeToSelect('small_image')
					->addStoreFilter(Mage::app()->getStore()->getId())
					->joinField('position',
						'catalog/category_product',
						'position',
						'product_id=entity_id',
						'category_id=' . $catId,
						'left')
					->setOrder('position', 'desc')
					->setPageSize(6)
					->setCurPage(1);
				$collection->addCategoryFilter($category);
			}
			
	        Mage::getSingleton('catalog/product_status')->addSaleableFilterToCollection($collection);
			Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($collection);
	        Mage::getSingleton('cataloginventory/stock')->addInStockFilterToCollection($collection);
	        $products = array();
	        foreach($collection as $item)
	        {
	        	$item->load($item->getId());
	        	$products[] = array(
	        		'id'=>$item->getId(),
	        		'name'=>$item->getName(),
	        		'tag'=>$item->getAttributeText('tag'),
	        		'price'=>$item->getPrice()+0,
	        		'special_price'=>$item->getFinalPrice()+0,
	        		'image'=>$_imageHelper->init($item, 'small_image')->resize(250)."",
	        		'position'=>$item->getPosition(),
	        	);
	        }

	        $data[] = array(
	        	'id'=>$category->getId(),
	        	'name'=>$category->getName(),
	        	'products'=>$products
	        );
		}

		apc_add($key, $data, 300);
		$this->render($data);
	}

	public function aboutAction()
	{
		$cms = Mage::getModel('cms/page')
			->setStoreId(Mage::app()->getStore()->getId())
			->load('about-magento-demo-store', 'identifier');
		if($cms && $cms->getId())
		{
			$content = $cms->getContent();
			$content = Mage::helper('cms')->getBlockTemplateProcessor()->filter($content);
		}
		else
		{
			$content = '';
		}
		return $this->render(array('content'=>$content));
	}
}